package com.wanxi.service.impl;

import com.wanxi.mapper.TeamMapper;
import com.wanxi.model.Common;
import com.wanxi.model.Message;
import com.wanxi.model.Team;
import com.wanxi.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    TeamMapper teamMapper;
    @Override
    public Message<Team> getTeamList(Map map) {
        int page = Integer.parseInt((String) map.get("page"));
        int size = Integer.parseInt((String) map.get("size"));
        map.put("page",(page-1)* size);
        map.put("size",size);
        Message message = new Message();
        message.setList(teamMapper.getTeamList(map));
        message.setTotal(teamMapper.getTeamCount(map));
        message.setModel(map);
        if (message.getList()!=null)
            message.setCode(200);
        else
            message.setCode(500);
        return message;
    }

    @Override
    public int addTeam(Team team) {
        int result = teamMapper.addTeam(team);
        if (result>0)
            return 200;
        else
            return 500;
    }

    @Override
    public Message getTeamByCode(String code) {
        Message message = new Message();
        Map<String,Object> map = new HashMap();
        Team team = teamMapper.getTeamByCode(code);
        if (team!=null){
            message.setCode(200);
        }else
            message.setCode(500);
        map.put("team",team);
        message.setModel(map);
        return message;
    }

    @Override
    public int editTeam(Map map) {
        int result = teamMapper.editTeam(map);
        if (result>0)
            return 200;
        else
            return 500;
    }

    @Override
    public void freshTeamCrew(String code) {
        teamMapper.freshTeamCrew(code);
    }

    @Override
    public Message getTeamByLeader(String leader) {
        Message message = new Message();
        Map map = new HashMap();
        Team team = teamMapper.getTeamByLeader(leader);
        if (team!=null){
            message.setCode(200);
        }else
            message.setCode(500);
        map.put("team",team);
        message.setModel(map);
        return message;
    }

    @Override
    public int deleteTeam(Integer id) {
        int result = teamMapper.deleteTeam(id);
        if (result>0)
            return 200;
        else
            return 500;
    }

    @Override
    public List<Team> getAllTeam() {
        return teamMapper.getAllTeam();
    }
}
